package com.coolbi.security.biz.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.security.biz.RoleToUserBiz;
import com.coolbi.security.dao.RoleToUserDao;
import com.coolbi.security.entity.RoleToUser;
import com.coolbi.security.entity.Roles;
import com.coolbi.security.entity.User;

public class RoleToUserBizImpl implements RoleToUserBiz {

	@Autowired
	private RoleToUserDao roleToUserDao;
	public void setRoleToUserDao(RoleToUserDao roleToUserDao) {
		this.roleToUserDao = roleToUserDao;
	}
	@Override
	public List<RoleToUser> find(Map<String, Object> parameter) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<RoleToUser> find(Map<String, Object> parameter, int offset,
			int limit) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int count(Map<String, Object> parameter) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public int countByKeyword(String keyword) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public List<RoleToUser> search(String keyword, int offset, int limit) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public RoleToUser save(RoleToUser obj) {
		return roleToUserDao.save(obj);
	}
	@Override
	public int update(RoleToUser obj) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public int delete(long id) {
		return roleToUserDao.delete(id);
	}
	@Override
	public RoleToUser get(long id) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<RoleToUser> list() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<RoleToUser> list(int offset, int limit) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int count() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public List<Roles> findRoleByUserId(long id) {
		return roleToUserDao.findRoleByUserId(id);
	}
	@Override
	public List<User> findUserByRoleId(long id) {
		return roleToUserDao.findUserByRoleId(id);
	}
	@Override
	public int deleteByRoleId(long id) {
		return roleToUserDao.deleteByRoleId(id);
	}
	@Override
	public List<Roles> findGroupRoleByUserId(long id) {
		return roleToUserDao.findGroupRoleByUserId(id);
	}
	@Override
	public void saveRoleAuthToUser(Long[] authArray, Long[] abolishArray,
			RoleToUser roleToUser) {
		if (null != abolishArray) {
			roleToUserDao.deleteByRoleIds(abolishArray, roleToUser.getUserId());
		}
		if (null != authArray) {
			List<Long> roleIds = roleToUserDao.findByRolesAndUserId(authArray, roleToUser.getUserId());
			for (int i = 0; i < authArray.length; i ++) {
				if (roleIds.size() > 0) {
					if (!roleIds.contains(authArray[i])) {
						roleToUser.setRoleId(authArray[i]);
						roleToUserDao.save(roleToUser);
					}
				} else {
					roleToUser.setRoleId(authArray[i]);
					roleToUserDao.save(roleToUser);
				}
			}
		}
	}
}
